﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using WFNetLib;
using System.Windows.Forms;

namespace YouJiDuQu
{
    public class SerialCommunication
    {        
        private SerialPort _COM = null;
        string _portName;
        public SerialCommunication(string portName)
        {
            _portName = portName;            
        }       
        public void CloseSerialPort()
        {
            _COM.Close();
            _COM.Dispose();
        }
        public bool OpenPort(string portName)
        {
            _portName = portName;
            if (_COM != null && _COM.IsOpen)
            {
                _COM.Close();
            }
            _COM = new SerialPort(_portName, 9600, Parity.None, 8, StopBits.Two);
            _COM.ReadTimeout = 500;
            while (true)
            {
                _COM.Close();
                try
                {                    
                    _COM.Open();
                    return true;
                }
                catch
                {
                    if(MessageBox.Show("当前串口无法打开,是否重试","通信错误",MessageBoxButtons.YesNo)==DialogResult.No)
                        return false;
                }                
            }            
        }
        public bool GetRet(byte Addr, ushort needLen, ref byte[] RxList)
        {            
            byte[] Tx=new byte[8]; 
            Tx[0]=Addr;
            Tx[1]=0x03;
            Tx[2]=0x00;
            Tx[3]=0x10;
            Tx[4]=0x00;
            Tx[5]=0x09;
            ushort crc16=Verify.GetVerify_CRC16(Tx,6);
            Tx[7] = BytesOP.GetLowByte(crc16);
            Tx[6] = BytesOP.GetHighByte(crc16);
            _COM.Write(Tx, 0, 8);            
            ushort RxCount = 0;
            int retry = 100;            
            while (retry != 0)
            {
                try
                {
                    RxList[RxCount++] = (byte)_COM.ReadByte();                    
                }
                catch/* (TimeoutException ex)*/
                {
                    return false;
                }
                if (RxCount == 1)
                {
                    if (RxList[0] != Addr)
                    {
                        RxCount = 0;
                        retry--;
                    }
                }
                else if (RxCount == 3)//应该已经收到了长度
                {
                    if (needLen != (byte)(RxList[2]+5))
                    {
                        RxCount--;
                        retry--;
                    }                    
                }
                else if (RxCount > 3)//判断是否接收完成
                {
                    if (RxCount == 23)
                    {
                        crc16 = Verify.GetVerify_CRC16(RxList, 21);
                        if (crc16 == BytesOP.MakeShort(RxList[21], RxList[22]))
                            break;
                        else
                            return false;
                    }
                }
            }            
            return true;
        }        
    }
}
